package com.lyh.poi.controller.poi;

import com.alibaba.fastjson.JSON;
import com.lyh.poi.common.utils.AjaxResult;
import com.lyh.poi.common.utils.ExcelUtil;
import com.lyh.poi.vo.PoiTestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyanhua@gogpay.cn
 * @date 2023/7/17 16:17
 */
@Api(tags = "poi导入导出")
@RestController
@RequestMapping("/poi")
public class PoiController {


    @ApiOperation(value = "支持图片导出")
    @PostMapping("/export")
    public AjaxResult export() {
        ExcelUtil<PoiTestVO> util = new ExcelUtil<PoiTestVO>(PoiTestVO.class);
        List<PoiTestVO> list = new ArrayList<>();
        PoiTestVO poiTestVO = new PoiTestVO();
        poiTestVO.setName("张三");
        poiTestVO.setLogo("https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg");
        list.add(poiTestVO);
        AjaxResult test = util.exportExcel(list, "test");
        return test;
    }

    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public void excelImport(MultipartFile file) {
        ExcelUtil<PoiTestVO> util = new ExcelUtil<PoiTestVO>(PoiTestVO.class);
        List<PoiTestVO> list = null;
        try {
            list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(list));
    }

}
